/* eslint-disable no-continue */
/*
 * @Author: liuyonghu
 * @Date: 2022-08-12 17:13:17
 * @LastEditTime: 2023-11-15 10:25:43
 * @LastEditors: liuyonghu
 * @Description: request ulits
 * @FilePath: \as-front\src\requests\utils.ts
 */

type NullObj = {
  key: string;
};
const nullVals = ['', null];

function isNull(val: any) {
  // 删除 {}
  return nullVals.includes(val) ;
  // return nullVals.includes(val);
}

function isNullObj(val: any) {
  // 删除 {}
  return  JSON.stringify(val) === '{}';
  // return nullVals.includes(val);
}

function getNullPath(
  nullKeys: Array<NullObj>,
  keys: Array<string>,
  params: any,
  firstKey: string
) {
  for (let index = 0; index < keys.length; index += 1) {
    const key = keys[index];
    const val = params[key];
    const keyPath = firstKey
      ? ''.concat(firstKey, ',').concat(key)
      : ''.concat(key);
    // debugger;
    if (isNull(val)|| isNullObj(val)) {
      const keyObj = nullKeys.find((it) => it.key === keyPath);
      if (!keyObj) {
        nullKeys.push({
          key: keyPath,
        });
      }
      continue;
    }
    if (Object.prototype.toString.call(val) === '[object Object]') {
      const subKeys = Object.keys(val);
      if (subKeys.length > 0) {
        getNullPath(nullKeys, subKeys, val, keyPath);
      } else {
        // 删除 {}
        nullKeys.push({
          key: keyPath,
        });
      }
    }
  }
}

function getAllNullKeys(params: Object) {
  const keys = Object.keys(params);
  const nullKeys: NullObj[] = [];
  if (keys.length > 0) {
    getNullPath(nullKeys, keys, params, '');
  }
  return nullKeys;
}
export function getValByPath(params: any, pathObj: NullObj) {
  const keys = pathObj.key.split(',');
  let obj = params;
  for (let i = 0; i < keys.length; i += 1) {
    const key = keys[i];
    if (i === keys.length - 1) {
      return obj[key];
    }
    obj = obj[key];
  }
}

function deleteNullByPath(params: any, pathObj: NullObj, level: number) {
  const keys = pathObj.key.split(',');
  let obj = params;
  debugger;
  if (keys.length >= level) {
    for (let i = 0; i < keys.length; i += 1) {
      const key = keys[i];
      if (i === keys.length - 1) {
        delete obj[key];
        continue;
      }
      obj = obj[key];
    }
  }
}

function handleNullByPath(params: any, pathObj: NullObj, level: number) {
  // console.log(pathObj);
  deleteNullByPath(params, pathObj, level);
  const keys = pathObj.key.split(',');
  if (keys.length > 1) {
    keys.pop();
    const nexKey = keys.join(',');
    const val = getValByPath(params, { key: nexKey });
    if (isNull(val) || isNullObj(val)) {
      handleNullByPath(params, { key: nexKey }, level);
    }
  }
}

export function deleteNull(params: any, level: number = 2) {
  console.log(' deleteNull params = ', JSON.stringify(params, null, '\t'));
  const nullPaths = getAllNullKeys(params);
  console.log(
    ' deleteNull nullPaths = ',
    JSON.stringify(nullPaths, null, '\t')
  );
  // debugger;
  for (let index = 0; index < nullPaths.length; index += 1) {
    const keyPath = nullPaths[index];
    handleNullByPath(params, keyPath, level);
  }
}

export default { deleteNull };
